package com.magic.smartcity.view.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.Navigation;

import com.magic.smartcity.R;
import com.magic.smartcity.beans.UserBean;
import com.magic.smartcity.databinding.FragmentMeLoginBinding;
import com.magic.smartcity.interfaces.IUserPresenter;
import com.magic.smartcity.interfaces.base.BaseUserCallBackFragment;
import com.magic.smartcity.utils.Constant;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * @description:
 * @create: Create by Magic on 2022/2/7.
 */
@AndroidEntryPoint
public class MeLoginFragment extends BaseUserCallBackFragment {
    private static final String TAG = MeLoginFragment.class.getSimpleName();

    FragmentMeLoginBinding mBinding;

    @Inject
    IUserPresenter mPresenter;
    private SharedPreferences mSharedPreferences;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = FragmentMeLoginBinding.inflate(getLayoutInflater());

        mSharedPreferences = getActivity().getSharedPreferences("login-user", Context.MODE_PRIVATE);
        mPresenter.registerCallBack(this);
        initEvent();
        return mBinding.getRoot();
    }

    private void initEvent() {
        mBinding.login.setOnClickListener(v -> mPresenter.loginUser(mBinding.username.getText().toString(), mBinding.password.getText().toString()));
        mBinding.register.setOnClickListener(v -> Navigation.findNavController(mBinding.getRoot()).navigate(R.id.action_loginFragment_to_registerFragment));
    }


    @Override
    public void onResume() {
        super.onResume();
        if (mSharedPreferences.getString("token", null) != null) {
            Toast.makeText(getActivity().getBaseContext(), "已登录返回主页", Toast.LENGTH_SHORT).show();
            Navigation.findNavController(mBinding.getRoot()).popBackStack();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unRegisterCallBack(this);
    }

////////////////接口回调

    @Override
    public void getUserLoaded(UserBean.UserBeanDo user) {

    }

    @Override
    public void loginOk(String token) {
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("login-user", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("token", token);
        edit.apply();
        Constant.TOKEN = token;
        Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
        Navigation.findNavController(mBinding.getRoot()).popBackStack();
    }

    @Override
    public void error(String str) {
        Toast.makeText(getActivity().getBaseContext(), str, Toast.LENGTH_SHORT).show();
    }
}
